<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<azthink@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Menu as MenuModel;

/**
 * 菜单控制器
 *
 * @author 李忠<azthink@qq.com>
 * @createAt 2018-08-31 18:11:41
 *
 */
class Menu extends Admin {

    /**
     * 列表页面
     */
    public function index($module = 'admin', $pid = 0, $title = null) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = MenuModel::lists($module, $pid, $title);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('menu_tree', list_to_config(MenuModel::toFormatTree('admin'), 'id', 'title_show'));

        $this->assign('module', $module);
        $this->assign('pid', $pid);
        $this->assign('ppid', MenuModel::getPid($pid));
        $this->assign('title', $title);
        $this->setMeta('菜单管理');
        return $this->fetch();
    }

    // 新增菜单
    public function add() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Menu');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = MenuModel::create($data)) {
                session('top_menus', null);
                session('sub_menus', null);
                $this->log($data['id'], '新增菜单成功');
                $this->success('新增菜单成功', cookie('__forward__'));
            } else {
                $this->error('新增菜单失败');
            }
        } else {
            $info = [
                'id' => null,
                'pid' => input('pid'),
                'type' => null,
                'title' => null,
                'module' => null,
                'group' => null,
                'icon' => null,
                'url' => null,
                'is_dev' => null,
                'hide' => null,
                'sort' => null,
                'is_menu' => null,
                'remark' => null
            ];


            $this->assign('menu_tree', list_to_config(MenuModel::toFormatTree('admin'), 'id', 'title_show'));
            $this->assign('menu_module', config('module_list'));
            $this->assign('yes_no', config('yes_no'));
            $this->assign('info', $info);
            $this->setMeta('新增菜单');
            return $this->fetch();
        }
    }

    // 更新菜单
    public function edit($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Menu');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = MenuModel::update($data)) {
                session('top_menus', null);
                session('sub_menus', null);
                $this->log($data['id'], '更新菜单成功');
                $this->success('更新菜单成功', cookie('__forward__'));
            } else {
                $this->error('更新菜单失败');
            }

        } else {
            $info = MenuModel::get($id);
            $this->assign('menu_tree', list_to_config(MenuModel::toFormatTree('admin'), 'id', 'title_show'));
            $this->assign('menu_module', config('module_list'));
            $this->assign('yes_no', config('yes_no'));

            $this->assign('info', $info);
            $this->setMeta('更新菜单');
            return $this->fetch('add');
        }
    }

    /**
     * 排序
     */
    public function sort($pid = 0) {
        if ($this->request->isPost()) {
            $ids = input('ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key => $value) {
                MenuModel::where('id', $value)->update(['sort' => $key + 1]);
            }

            $this->success('菜单排序成功！');

        } else {
            // 获取排序的数据
            $map = array(['status', '=', 1], ['pid', '=', $pid]);

            $data_list = MenuModel::where($map)->field('id,title')->order('sort asc')->select();
            $this->assign('data_list', $data_list);
            $this->setMeta('菜单排序');
            return $this->fetch();
        }
    }


    public function delete($id) {
        if (MenuModel::state($id, -1)) {
            $this->log($id, '删除菜单成功');
            $this->success('删除菜单成功');
        } else {
            $this->error('删除菜单失败');
        }
    }
}